/* 
*   项目中所有的测试代码 和 实例代码
*/


// #include <iostream>
// #include <string>

// #include "level.hpp"
// #include "util.hpp"
// #include "format.hpp"
// #include "sink.hpp"
// #include "logger.hpp"
// #include <vector>
// #include <fstream>
//#include "buffer.hpp"
#include "../LogSystemCode/mylog.h"
//using namespace std;

//测试util.hpp
// int main()
// {
//     cout<<log::util::Date::now()<<endl;
//     string fpath = "./abc/def/x.txt";
//     log::util::File::createDirectory(log::util::File::path(fpath));
//     return 0;
// }

//测试level.hpp
// int main()
// {
//     cout<<Log::LogLevel::toString(Log::LogLevel::value::DEBUG)<<endl;
//     cout<<Log::LogLevel::toString(Log::LogLevel::value::INFO)<<endl;
//     cout<<Log::LogLevel::toString(Log::LogLevel::value::ERROR)<<endl;
//     cout<<Log::LogLevel::toString(Log::LogLevel::value::WARN)<<endl;
//     cout<<Log::LogLevel::toString(Log::LogLevel::value::FATAL)<<endl;
//     cout<<Log::LogLevel::toString(Log::LogLevel::value::OFF)<<endl;
//     cout<<Log::LogLevel::toString(Log::LogLevel::value::UNKNOW)<<endl;
//     return 0;
// }

//测试message.hpp
// int main()
// {
//     Log::LogMsg msg(Log::LogLevel::value::DEBUG,__LINE__,__FILE__,"root","检测");
//     Log::FormatItem::ptr fmt(new Log::LevelFormatItem);
//     fmt.reset(new Log::FileFormatItem);
//     fmt.reset(new Log::TimeFormatItem("%Y-%m-%d|%H:%M:%S"));
//     fmt.reset(new Log::LoggerFormatItem);
//     fmt.reset(new Log::MsgFormatItem);

//     fmt->format(cout,msg);
//     return 0;
// }

//测试formater.hpp
// int main()
// {
//     Log::LogMsg msg(Log::LogLevel::value::DEBUG,__LINE__,__FILE__,"root","检测");
//     Log::Formatter fmter("[%d{%Y-%m-%d|%H:%M:%S}][%t][%c][%f:%l][%p]%T%m%n");
//     std::cout<<fmter.format(msg);
    
//     return 0;
// }

//测试sink.hpp
// int main()
// {
//     Log::LogMsg msg(Log::LogLevel::value::DEBUG,__LINE__,__FILE__,"root","检测");
//     Log::Formatter fmter;
//     std::string logmsg = fmter.format(msg);
//     Log::LogSink::ptr std_log = Log::SinkFactor::create<Log::StdoutSink>(); //标准输出
//     Log::LogSink::ptr file_log = Log::SinkFactor::create<Log::FileSink>("./log/test.log"); //文件流输出
//     Log::LogSink::ptr Roll_log = Log::SinkFactor::create<Log::RollBySizeSink>("./log/base-",1024*1024); //滚动文件输出 默认1M就切换文件
//     std_log->log(logmsg.c_str(),logmsg.size());
//     file_log->log(logmsg.c_str(),logmsg.size());
//     int cur_size = 0;
//     int count = 0;
//     while(cur_size <= 1024*1024*10) //写入10M日志
//     {
//         std::string tmplog = to_string(count++)+logmsg;
//         Roll_log->log(tmplog.c_str(),tmplog.size());
//         cur_size += tmplog.size();
//     }
//     return 0;
// }


//测试sink.hpp 可扩展的支持
//按天数滚动文件
// enum class TimeGap //滚动大小
// {
//     GAP_SECOND, //以秒为步数
//     GAP_MINUTE, //分
//     GAP_HOUR,   //时
//     GAP_DAY     //天
// };

//同步日志器测试
// int main()
// {
//     Log::LogSink::ptr std_log = Log::SinkFactor::create<Log::StdoutSink>();
//     Log::LogSink::ptr file_log = Log::SinkFactor::create<Log::FileSink>("./log/test.log");
//     Log::LogSink::ptr Roll_log = Log::SinkFactor::create<Log::RollBySizeSink>("./log/base-",1024*1024); //以1M滚动
//     std::vector<Log::LogSink::ptr> sinks = {std_log,file_log,Roll_log};
//     Log::SyncLogger::ptr logger(new Log::SyncLogger("测试日志器",Log::LogLevel::value::WARN,std::make_shared<Log::Formatter>(),sinks));

//     logger->debug(__FILE__,__LINE__,"测试日志-%d",-1);
//     logger->info(__FILE__,__LINE__,"测试日志-%d",-1);
//     logger->warn(__FILE__,__LINE__,"测试日志-%d",0);
//     logger->error(__FILE__,__LINE__,"测试日志-%d",0);
//     logger->fatal(__FILE__,__LINE__,"测试日志-%d",0);
//     int cur_size = 0;
//     int count = 1;
//     while(cur_size < 1021*1024*5)
//     {
//         logger->fatal(__FILE__,__LINE__,"测试日志-%d",count++);
//         cur_size += 48;
//     }
//     return 0;
// }

//局部同步日志器建造测试
// int main()
// {
//     std::unique_ptr<Log::LocalLoggerBuilder> logbuild(new Log::LocalLoggerBuilder);
//     logbuild->buildFormatter("[%d{%Y-%m-%d|%H:%M:%S}][%t][%c][%f:%l][%p]%T%m%n");
//     logbuild->buildLoggerLevel(Log::LogLevel::value::WARN);
//     logbuild->buildLoggerName("测试器");
//     logbuild->buildSink<Log::StdoutSink>();
//     logbuild->buildSink<Log::FileSink>("./log/test.log");
//     Log::Logger::ptr logger = logbuild->build();

//     logger->debug(__FILE__,__LINE__,"测试日志-%d",-1);
//     logger->info(__FILE__,__LINE__,"测试日志-%d",-1);
//     logger->warn(__FILE__,__LINE__,"测试日志-%d",0);
//     logger->error(__FILE__,__LINE__,"测试日志-%d",0);
//     logger->fatal(__FILE__,__LINE__,"测试日志-%d",0);
//     int cur_size = 0;
//     int count = 1;
//     while(cur_size < 1021*1024*5)
//     {
//         logger->fatal(__FILE__,__LINE__,"测试日志-%d",count++);
//         cur_size += 48;
//     }
//     return 0;
// }

//缓冲区测试
// int main()
// {
//     std::ifstream ifs("./log/test.log",std::ios::binary);
//     if(ifs.is_open() == false) { std::cout<<"open error\n"; return -1; }  
//     ifs.seekg(0,std::ios::end); //流指针移动到文件末尾
//     int sz = ifs.tellg(); //获取当前流指针相当于文件开始的偏移量
//     ifs.seekg(0,std::ios::beg); //流指针回到文件开始
//     std::string logstr;
//     logstr.resize(sz);
//     ifs.read(&logstr[0],sz); //写入日志
//     if(ifs.good() == false) { std::cout<<"read error\n"; return -2; }
//     ifs.close();

//     std::cout<<sz<<std::endl;
//     std::cout<<logstr.size()<<std::endl;

//     int n = logstr.size();
//     Log::Buffer buf;
//     for(int i = 0;i<n;++i)
//     {
//         buf.push(&logstr[i],1); //每次写入一个字节
//     }
//     n = buf.readAbleSize();
//     std::ofstream ofs("./log/tmp.log",std::ios::binary);
//     for(int i = 0;i<n;++i)
//     {
//         //写入一个字节
//         ofs.write(buf.begin(),1);
//         //读取指针向后偏移
//         buf.moveReadPos(1);
//     }
//     return 0;
// }

//建造者模式异步日志器测试
// int main()
// {
//     std::unique_ptr<Log::LocalLoggerBuilder> logbuild(new Log::LocalLoggerBuilder);
//     logbuild->buildLoggerType(Log::LoggerType::LOGGER_ASYNC); //设置为异步日志器
//     logbuild->buildAsyncUnsafe(); //设置为不安全模式
//     logbuild->buildFormatter("[%d{%Y-%m-%d|%H:%M:%S}][%t][%c][%f:%l][%p]%T%m%n");
//     logbuild->buildLoggerLevel(Log::LogLevel::value::WARN);
//     logbuild->buildLoggerName("测试器");
//     logbuild->buildSink<Log::StdoutSink>();
//     logbuild->buildSink<Log::FileSink>("./log/test.log");
//     Log::Logger::ptr logger = logbuild->build();

//     logger->debug(__FILE__,__LINE__,"测试日志-%d",-1);
//     logger->info(__FILE__,__LINE__,"测试日志-%d",-1);
//     logger->warn(__FILE__,__LINE__,"测试日志-%d",0);
//     logger->error(__FILE__,__LINE__,"测试日志-%d",0);
//     logger->fatal(__FILE__,__LINE__,"测试日志-%d",0);

//     int count = 0;
//     while(count < 500000)
//         logger->fatal(__FILE__,__LINE__,"测试日志-%d",count++);
    
//     return 0;
// }

//全局日志器和日志器管理器测试
// void test_log()
// {
//     Log::Logger::ptr logger = Log::LoggerManager::getInstance().getLogger("测试器");

//     logger->debug(__FILE__,__LINE__,"测试日志-%d",-1);
//     logger->info(__FILE__,__LINE__,"测试日志-%d",-1);
//     logger->warn(__FILE__,__LINE__,"测试日志-%d",0);
//     logger->error(__FILE__,__LINE__,"测试日志-%d",0);
//     logger->fatal(__FILE__,__LINE__,"测试日志-%d",0);

//     int count = 0;
//     while(count < 500000)
//         logger->fatal(__FILE__,__LINE__,"测试日志-%d",count++);
// }

// int main()
// {
//     std::unique_ptr<Log::LoggerBuilder> logbuild(new Log::GlobalLoggerBuilder);
//     logbuild->buildLoggerType(Log::LoggerType::LOGGER_ASYNC); //设置为异步日志器
//     logbuild->buildAsyncUnsafe(); //设置为不安全模式
//     logbuild->buildFormatter("[%d{%Y-%m-%d|%H:%M:%S}][%t][%c][%f:%l][%p]%T%m%n");
//     logbuild->buildLoggerLevel(Log::LogLevel::value::WARN);
//     logbuild->buildLoggerName("测试器");
//     logbuild->buildSink<Log::StdoutSink>();
//     logbuild->buildSink<Log::FileSink>("./log/test.log");
//     Log::Logger::ptr logger = logbuild->build();
//     test_log();


//     return 0;
// }

//全局接口和宏测试 - 实际使用示例代码
void test_log(const std::string& name)
{
    DEFINFO("测试开始");
    Log::Logger::ptr logger = Log::LoggerManager::getInstance().getLogger(name);

    //写100条日志测试所有接口
    int n = 100;
    while(n--)
    {
        logger->DEBUGLOG("测试日志-%d",0);
        logger->INFOLOG("测试日志-%d",0);
        logger->WARNLOG("测试日志-%d",0);
        logger->ERRORLOG("测试日志-%d",0);
        logger->FATALLOG("测试日志-%d",0);
    }

    DEFINFO("测试结束");
}

int main()
{
    std::unique_ptr<Log::LoggerBuilder> logbuild(new Log::GlobalLoggerBuilder);

    //异步日志器在切换文件时存在未刷新切换的问题 实际是size还在缓冲区会丢失
    logbuild->buildLoggerType(Log::LoggerType::LOGGER_ASYNC); //设置为异步日志器
    logbuild->buildAsyncUnsafe(); //设置为不安全模式
    logbuild->buildFormatter("[%d{%Y-%m-%d|%H:%M:%S}][%t][%c][%f:%l][%p]%T%m%n");
    logbuild->buildLoggerLevel(Log::LogLevel::value::WARN);
    logbuild->buildLoggerName("测试器");
    logbuild->buildSink<Log::StdoutSink>();
    logbuild->buildSink<Log::FileSink>("./log/test.log");
    logbuild->buildSink<Log::RollBySizeSink>("./log/base-",1024*1024); //1M 滚动
    logbuild->build();
    test_log("测试器");

    return 0;
}